package com.android.jianboshi.ui.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.fragment.app.FragmentActivity;

import com.android.baselibrary.dialog.CommList_DialogFragment;
import com.android.baselibrary.dialog.Comm_DialogFragment;
import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.tool.Log;
import com.android.baselibrary.tool.SPUtil;
import com.android.baselibrary.ui.BaseFragment;
import com.android.baselibrary.util.Util;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.baselibrary.view.Comm_TitleView;
import com.android.jianboshi.R;
import com.android.jianboshi.constants.SPConstants;
import com.android.jianboshi.constants.URLConstants;
import com.android.jianboshi.manager.LanguageManager;
import com.android.jianboshi.ui.activity.AboutUsActivity;
import com.android.jianboshi.ui.activity.Device_ListActivity;
import com.android.jianboshi.ui.activity.MainActivity;
import com.android.jianboshi.ui.activity.SettingActivity;
import com.android.jianboshi.ui.activity.TestActivity;
import com.android.jianboshi.ui.activity.TestActivity1;
import com.android.jianboshi.ui.activity.WebViewActivity;
import com.android.jianboshi.utils.LocaleUtils;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeMineFragment extends BaseFragment {

    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;
    @BindView(R.id.ct_version)
    Comm_TitleView ct_version;
    @BindView(R.id.ct_devicelist)
    Comm_TitleView ct_devicelist;
    @BindView(R.id.ct_language)
    Comm_TitleView ct_language;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        comm_title.setLeftImgVisible(View.GONE);
        comm_title.setTitle(getString(R.string.mine_title));
        ImmersionBar.with(this).titleBar(comm_title).statusBarDarkFont(false).navigationBarDarkIcon(false).init();

        ct_version.setContent("v " + Util.getVersionName(mContext));

        ct_language.setContent(LanguageManager.getCurrentLanguageName());

    }

    @Override
    public void onResume() {
        super.onResume();
        if (mContext != null) {
            String Mac = SPUtil.getStringValue(mContext, SPConstants.DeviceName, "");
            ct_devicelist.setContent(Mac);
        }
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.ct_xieyi, R.id.ct_about, R.id.ct_setting, R.id.ct_test, R.id.ct_test1, R.id.ct_bindmac, R.id.ct_devicelist, R.id.ct_language})
    public void onViewClicked(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.ct_xieyi:
                //判断语言，加载不同的用户协议
                Locale locale = LanguageManager.getCurrentLocale();
                Log.i("locale:" + locale.getLanguage());
                String filePath = "";
                if (locale.getLanguage().equals(Locale.CHINESE.getLanguage())) {
                    filePath = "file:///android_asset/yinsixieyi.html";
                } else if (locale.getLanguage().equals(Locale.ENGLISH.getLanguage())) {
                    filePath = "file:///android_asset/yinsixieyi_en.html";
                } else if (locale.getLanguage().equals(new Locale("es").getLanguage())) {
                    filePath = "file:///android_asset/yinsixieyi_es.html";
                } else if (locale.getLanguage().equals(Locale.FRENCH.getLanguage())) {
                    filePath = "file:///android_asset/yinsixieyi_fr.html";
                } else if (locale.getLanguage().equals(Locale.GERMAN.getLanguage())) {
                    filePath = "file:///android_asset/yinsixieyi_de.html";
                } else if (locale.getLanguage().equals(new Locale("ru").getLanguage())) {
                    filePath = "file:///android_asset/yinsixieyi_ru.html";
                } else if (locale.getLanguage().equals(Locale.JAPANESE.getLanguage())) {
                    filePath = "file:///android_asset/yinsixieyi_ja.html";
                } else if (locale.getLanguage().equals(new Locale("pt").getLanguage())) {
                    filePath = "file:///android_asset/yinsixieyi_pt.html";
                } else {
                    filePath = "file:///android_asset/yinsixieyi_en.html";
                }
                WebViewActivity.startActivity(mContext, "", filePath);
                break;
            case R.id.ct_about:
                StartActivity(AboutUsActivity.class);
                break;
            case R.id.ct_setting:
                StartActivity(SettingActivity.class);
                break;
            case R.id.ct_test:
                StartActivity(TestActivity.class);
                break;
            case R.id.ct_test1:
                StartActivity(TestActivity1.class);
                break;
//            case R.id.ct_bindmac:
//                StartActivity(BindMacActivity.class);
//                break;
            case R.id.ct_devicelist:
                toBind();
                break;
            case R.id.ct_language:   //语言
                List<String> list1 = new ArrayList<>();
                list1.add(getString(R.string.language_default));
                list1.add(getString(R.string.language_zh));
                list1.add(getString(R.string.language_en));
                list1.add(getString(R.string.language_es));
                list1.add(getString(R.string.language_fr));
                list1.add(getString(R.string.language_de));
                list1.add(getString(R.string.language_ru));
                list1.add(getString(R.string.language_ja));
                list1.add(getString(R.string.language_pt));
                CommList_DialogFragment.showCommDialog(getActivity(), list1, true, new CommCallBack() {
                    @Override
                    public void onResult(Object obj) {
                        int index = (int) obj;
                        LanguageManager.setLanguageType(index);

//                        Comm_DialogFragment.showCommDialog((FragmentActivity) mContext, "", getString(R.string.tips_restart), getString(R.string.btn_ok), "", new View.OnClickListener() {
//                            @Override
//                            public void onClick(View view) {
//                                System.exit(0);
//                            }
//                        },null);

                        LocaleUtils.changeAppLanguage(getActivity(), LanguageManager.getCurrentLocale(), MainActivity.class);
                    }
                }, null);
                break;
        }
    }

    //点击绑定
    private void toBind() {
        String Mac = SPUtil.getStringValue(mContext, SPConstants.DeviceName, "");
        if (TextUtils.isEmpty(Mac)) {
            //未绑定，直接跳设备列表
            StartActivity(Device_ListActivity.class);
        } else {
            List<String> list = new ArrayList<>();
            list.add(getString(R.string.bind_unbind));
            list.add(getString(R.string.bind_rebind));
            CommList_DialogFragment dialogFragment = (CommList_DialogFragment) CommList_DialogFragment.showCommDialog(getActivity(), list, true, new CommCallBack() {
                @Override
                public void onResult(Object obj) {
                    int index = (int) obj;
                    if (index == 0) {
                        SPUtil.putValue(mContext, SPConstants.DeviceName, "");
                        onResume();
                    } else {
                        StartActivity(Device_ListActivity.class);
                    }
                }
            }, null);
        }
    }
}
